package com.ruoyi.file.component;

import com.ruoyi.file.domain.FilePublishDto;
import com.ruoyi.file.domain.TblFile;
import com.ruoyi.file.service.ITblFileService;
import com.ruoyi.file.task.PublishLayerTask;
import com.ruoyi.gis.geoserver.service.GeoserverService;
import com.ruoyi.gis.domain.GisLayer;
import com.ruoyi.gis.service.IGisLayerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
public class PublishLayerProcess {
    private final Logger logger = LoggerFactory.getLogger(PublishLayerProcess.class);
    @Autowired
    @Qualifier("publishLayerTaskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private ITblFileService tblFileService;
    @Autowired
    private IGisLayerService gisLayerService;
    @Autowired
    private GeoserverService geoserverService;

    public void sendPublishLayerMessage(FilePublishDto filePublishDto) {
        if(filePublishDto==null||filePublishDto.getPublishFileId()==null
                ||filePublishDto.getParentLayerId()==null)return;
        taskExecutor.submit(new PublishLayerTask(filePublishDto));
    }

    public boolean cancelFilePublish(Long id) {
        TblFile tblFile = tblFileService.selectTblFileById(id);
        if(tblFile==null||tblFile.getPublished()==null||!tblFile.getPublished()||tblFile.getLayerId()==null){
            logger.info("图层未发布........");
            return false;
        }
        GisLayer gisLayer = gisLayerService.selectGisLayerByLayerId(tblFile.getLayerId());
        if(gisLayer==null)return false;
        logger.info("geoserver remove layer {} {} ",gisLayer.getWorkspace(),gisLayer.getLayerName());
        try {
            geoserverService.removeLayer(gisLayer.getStoreType(),
                    gisLayer.getWorkspace(),gisLayer.getStore(),
                    gisLayer.getLayerName());
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("delete layer error ................................");
        }
        logger.info("delete  layer info ");
        gisLayerService.deleteGisLayerByLayerId(gisLayer.getLayerId());
        logger.info("update file publish state ");
        //检查他的父下是否还有图层，如果没有，就删除该组
        gisLayerService.cleanLayerGroup(gisLayer.getParentId());
        tblFile.setPublished(false);
        tblFile.setLayerId(null);
        tblFileService.updateTblFile(tblFile);
        return true;
    }

    public void publishLayer(List<FilePublishDto> publishFilesMsg) {
        logger.info("the file is Publishable...........will publish layer count {}...............",publishFilesMsg.size());
        publishFilesMsg.stream().forEach(publishDto->{
            sendPublishLayerMessage(publishDto);
        });
        logger.info("saveFile...........end...............");
    }
}
